/*
 * ScriptHost.h
 *
 *  Created on: Oct 27, 2013
 *      Author: blackpc
 */

#ifndef SCRIPTHOST_H_
#define SCRIPTHOST_H_

#include <iostream>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/foreach.hpp>

#include <scriptable_analyzer/InternalFunction.h>
#include <scriptable_analyzer/PythonScript.h>
#include <scriptable_analyzer/PredicatesScript.h>
#include <scriptable_analyzer/ScriptExecuter.h>
#include <scriptable_analyzer/RosTopicListener.h>

#define foreach BOOST_FOREACH

using namespace std;

typedef boost::shared_ptr<diagnostic_msgs::DiagnosticStatus> DiagnosticStatusPtr;
//#define lock(mutexObject) boost::mutex::scoped_lock mutexObject##Lock(mutexObject)

class ScriptHost
{
public:
	ScriptHost();
	virtual ~ScriptHost();

	void start();
	void stop();

	void addScript(string sourceCode);
	vector<DiagnosticStatusPtr> getDiagnosticStatusesAndClear();

private:

	/**
	 * Scripts execution check interval in seconds
	 */
	double _executionInterval;
	boost::shared_ptr<boost::thread> _workThread;
	vector<PythonScriptPtr> _scripts;
	ScriptExecuter _executer;
	boost::mutex _scriptsMutex;
	boost::mutex _statusesMutex;
	vector<DiagnosticStatusPtr> _diagnosticStatuses;

	void run();
	bool prepareScript(PythonScript& script);
	bool isExecutionTime(PythonScriptPtr script);
	void addDiagnosticStatus(PythonScriptPtr script);

};

#endif /* SCRIPTHOST_H_ */
